##
## Supplement to TableA8.do 
## 
## This script processes the *unadjusted* survival and readmission outcomes
## at the hospital-year level into a stacked format where each row represents a 
## hospital-year-cohort 
##
## 




library(dplyr)
library(readstata13)
library(reshape2)
library(data.table)



# Read the file 

## Settings 
fdata = "/disk/agedisk4/medicare.work/sacarny-DUA51934/shruthi-dua51934/replication_files/survreadm/"

fpath_wd = "/disk/agedisk4/medicare.work/sacarny-DUA51934/shruthi-dua51934/replication_files/survreadm/"
fpath_revision ="/homes/nber/shruthi-dua51934/sacarny-DUA51934/shruthi-dua51934/replication_files/Revisions"
fpath_input = paste(fpath_wd, "input/", sep = "")
fpath_data = paste(fpath_revision, "/data/", sep= "")
fpath_output = paste(fpath_revision, "/output/", sep= "")
fpath_build_output = "/homes/nber/shruthi-dua51934/sacarny-DUA51934/shruthi-dua51934/replication_files/build/output/"

#filename_revisions_panel = "revisions_hosp_yr_panel.dta"


##
## create a stacked, hospital-year-cohort file based on stackedv2.csv (see survreadm/code/stack.R)
## 
makeStackedDataCurrent <- function() {
  
  ## process the files with unadjusted survival and readmission data into 
  ## id-year-cohort panel 
  filenames = paste(c("ami", "pnu", "chf", "hip", "stk"), "100_acq.dta", sep="")
  
  ## helper function to combine survival and readmission measures 
  proc_meas <- function(i, filenames) {
    
    # read the unadjusted survival and readmission measures for the given cohort
    d = data.table(read.dta13(paste(fpath_input, filenames[i], sep="")))
    keep = names(d)[str_detect(names(d), "^id$|^year$|rnra_.+fe$|snra_.+fe")]
    d = d[, ..keep]
    d = d[year %in% c(2006, 2010, 2014), ]
  
    # create a cohort variable
    cohort_name = str_replace(filenames[i], "100_acq.dta", "")
    d = d[, cohort := cohort_name]
    setnames(d, old = paste(c("snra_", "rnra_"), cohort_name, "_fe", sep=""), 
             new = c("snra", "rnra"))
    
    return(d)
  }
  meas_dt = purrr::map(seq(1, length(filenames),1), proc_meas, filenames) %>% 
    purrr::reduce(rbind)
  
  ## read in the old file, stackedv2.csv
  orig_dt = fread(paste(fpath_input, "stackedv2.csv", sep=""))
  #orig_dt = orig_dt[year %in% c(2006, 2010, 2014)]
  
  ## merge and clean up 
  output = merge.data.table(orig_dt, meas_dt, by = c("id", "year", "cohort"),
                         all.x=TRUE, all.y=FALSE)
  output = output[rhdx == ".", rhdx := NA]
  output = output[shdx == ".", shdx := NA]
  
  ## write to file 
  print("Writing stacked file with unadjusted measures")
  fwrite(output, paste(fpath_revision, "/data/stackedv2_unadj_meas.csv", sep =''))
  
}









